//
//  LogVC.m
//  qulvyou
//
//  Created by mmyy on 15/10/23.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "LogVC.h"

@interface LogVC ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation LogVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登陆";
    self.webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.webView.delegate = self;
    
    NSDictionary *dic = @{@"client_id":SINA_APP_KEY,@"redirect_uri":redirect_uri, @"display":@"mobile", @"response_type":@"token"};
    NSString *loginPath = [Help pathWithBasicPath:SINA_AUTHORIZE_SEVER andDic:dic];
    NSLog(@"%@", loginPath);
    NSURL *url = [NSURL URLWithString:loginPath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
    UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
    [left setTitle:@"back" forState:UIControlStateNormal];
    [left setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    left.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [left addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    // Do any additional setup after loading the view.
}
-(void)buttonClick
{
    if ([self.delegate respondsToSelector:@selector(logFaile)]) {
        [self.delegate logFaile];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSArray *arr = [request.URL.absoluteString componentsSeparatedByString:@"#"];
    if (arr.count == 2) {
        NSString *infoStr = arr[1];
        NSString *access_token = [Help getValueWithInfo:infoStr andKey:@"access_token="];
        NSString *expires_in = [Help getValueWithInfo:infoStr andKey:@"expires_in="];
        NSString *uid = [Help getValueWithInfo:infoStr andKey:@"uid="];
        
        [[NSUserDefaults standardUserDefaults] setObject:access_token forKey:ACCESS_TOKEN];
        [[NSUserDefaults standardUserDefaults] setObject:uid forKey:UID];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if (expires_in != nil) {
            
            NSTimeInterval time = [expires_in longLongValue];
            //            492758
            
            NSDate *futherDate = [NSDate dateWithTimeIntervalSinceNow:time];
            //            2015-10-26 19:00:01 +0000
            [[NSUserDefaults standardUserDefaults] setObject:futherDate forKey:EXPIRES_IN];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }
        [self downloadData];
        
        
        return NO;
    }
    return YES;
}
-(void)downloadData
{
    NSString *access_token = [[NSUserDefaults standardUserDefaults] objectForKey:ACCESS_TOKEN];
    NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:UID];
    NSString *urlPath = [Help pathWithBasicPath:Sine_search_User andDic:@{@"access_token":access_token, @"uid":uid}];
    [Funcation getWithURL:urlPath success:^(id responseObject) {
        NSString *screen_name = [responseObject objectForKey:@"screen_name"];
        NSString *prefile_image_url = [responseObject objectForKey:@"profile_image_url"];
        NSString *follwers_count = [[responseObject objectForKey:@"followers_count"] stringValue];
        NSString *friends_count = [[responseObject objectForKey:@"friends_count"] stringValue];
        [[NSUserDefaults standardUserDefaults] setValue:screen_name forKey:SCREEN_NAME];
        [[NSUserDefaults standardUserDefaults] setValue:prefile_image_url forKey:PROFILE_IMAGE_URL];
        [[NSUserDefaults standardUserDefaults] setValue:follwers_count forKey:FOLLOWERS_COUNT];
        [[NSUserDefaults standardUserDefaults] setValue:friends_count forKey:FRIENDS_COUNT];
        [self dismissViewControllerAnimated:YES completion:nil];
    } andFail:^(id error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"获取个人信息失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }];
    
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
